#!/usr/bin/env python
#
# Copyright (c) 2008 Ben Motmans <ben.motmans@gmail.com>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#

import os
import sys
import string
import shutil
import commands
import datetime
import tempfile

from configobj import ConfigObj

def svn_export (url, revision, export_dir):
	if (revision == None or revision == "HEAD" or revision == "trunk"):
		output = commands.getoutput ("svn export --quiet --non-interactive " + url + " " + export_dir)
	else:
		output = commands.getoutput ("svn export --quiet --non-interactive -r " + revision + " " + url + " " + export_dir)

	if output == "":
		return True
	else:
		"ERROR: unable to export sources"
		return False

def create_tarball (package_name, tmp_dir, output_dir):
	os.chdir (tmp_dir)

	tarball_name = package_name + ".tar"

	output = commands.getoutput ("tar -cf " + tarball_name + " " + package_name)
	if output != "":
		print "ERROR: unable to create tarball"
		return False

	output = commands.getoutput ("bzip2 -zfq9 " + tarball_name)
	if output != "":
		print "ERROR: unable to compress tarball"
		return False
	
	destfile = os.path.join (output_dir, tarball_name + ".bz2")

	if os.path.exists (destfile):
		os.remove (destfile)
	shutil.move (tarball_name + ".bz2", destfile)

def push_release (filename):
	if os.path.exists (filename) == False:
		print "File '" + filename + "' does not exist."
		sys.exit ()

	config = ConfigObj (filename)

	url = config["source"]["url"]
	revision = config["source"]["revision"]

	name = config["package"]["name"]
	version = config["package"]["version"]
	release = config["package"]["release"]

	if release == None or release == "":
		package_name = name + "-" + version
	elif release == "~":
		now = datetime.datetime.now ()
		release = "svn" + now.strftime ("%Y%m%d")
		package_name = name + "-" + version + "~" + release
	else:
		package_name = name + "-" + version + "~" + release

	tmp_dir = tempfile.mkdtemp ()
	export_dir = os.path.join (tmp_dir, package_name)
	output_dir = os.getcwd ()

	print "exporting to " + export_dir
	if svn_export (url, revision, export_dir) == False:
		return

	if create_tarball (package_name, tmp_dir, output_dir) == False:
		return

	shutil.rmtree (tmp_dir)

	print "done."

if len (sys.argv) < 2:
	print "Missing filename parameter."
	sys.exit ()

push_release (sys.argv[1])

